package com.dw.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dw.model.user.dtos.AddUserFollowDTO;
import com.dw.model.user.entitys.UserInfo;
import com.dw.model.user.vos.GetUserVO;
import com.dw.user.mapper.UserFollowMapper;
import com.dw.model.user.entitys.UserFollow;
import com.dw.user.service.UserFollowService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用户关注表(UserFollow)表服务实现类
 *
 * @author makejava
 * @since 2024-10-17 09:16:08
 */
@Service("userFollowService")
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private UserInfoServiceImpl userInfoService;

    private final String USER_FOLLOW_KEY = "user.follow";

    private final String USER_FOLLOW_COUNT_KEY = "user.follow.count";

    private final String USER_FOLLOW_DETAIL_KEY = "user.follow.detail";

    /**
     * 关注
a     */
    @Override
    public void follow(AddUserFollowDTO addUserFollowDTO) {
        // 获取登录用户id
        Long userId = addUserFollowDTO.getUserId();
        // 获取关注的用户id
        Long followId = addUserFollowDTO.getFollowId();
        Boolean isFollow = addUserFollowDTO.getIsFollow();
        String hashKey = userId + ":" + followId;
        redisTemplate.opsForHash().put(USER_FOLLOW_KEY, hashKey,isFollow ? 1 : 0);
        String countKey = USER_FOLLOW_COUNT_KEY + "." + userId;
        String detailKey = USER_FOLLOW_DETAIL_KEY + "." + userId + "." + followId;
        if(isFollow){
            // 执行关注业务
            redisTemplate.opsForValue().increment(countKey,1);
            redisTemplate.opsForValue().set(detailKey,1);
        } else {
            Number result = (Number) redisTemplate.opsForValue().get(detailKey);
            if(result == null || result.longValue() <= 0){
                return;
            }
            redisTemplate.opsForValue().increment(countKey,-1);
            redisTemplate.delete(detailKey);
        }
    }

    /**
     * 用户是否关注
     */
    @Override
    public Boolean isFollow(Long userId,Long followId) {
        String followKey = USER_FOLLOW_DETAIL_KEY + "." + userId + "." + followId;
        Number follow  = (Number) redisTemplate.opsForValue().get(followKey);
        if(follow != null && follow.longValue() > 0){
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取用户关注的所有用户
     */
    @Override
    public List<GetUserVO> getAllFollow(Long userId) {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(USER_FOLLOW_KEY);
        // 获取用户关注的所有用户id
        List<Long> collect = entries.entrySet().stream().filter(o -> {
            String[] keyArr = o.getKey().toString().split(":");
            Long userID = Long.valueOf(keyArr[0]);
            return userID.equals(userId) && o.getValue().toString().equals("1");
        }).map(o -> Long.valueOf(o.toString().split(":")[1].split("=")[0])).collect(Collectors.toList());
        // 根据id列表获取用户信息
        return collect.stream().map(followId -> {
            GetUserVO getUserVO = userInfoService.getUserById(followId);
            // 判断用户是否取关当前关注用户
            Boolean follow = isFollow(userId, followId);
            getUserVO.setGuanzhuFollow(follow);
            return getUserVO;
        }).collect(Collectors.toList());

    }

    /**
     * 获取用户的所有粉丝
     */
    @Override
    public List<GetUserVO> getAllFans(Long userId) {
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(USER_FOLLOW_KEY);
        // 获取用户的所有粉丝id
        List<Long> collect = entries.entrySet().stream().filter(o -> {
            String[] keyArr = o.getKey().toString().split(":");
            Long followId = Long.valueOf(keyArr[1]);
            return followId.equals(userId) && o.getValue().toString().equals("1");
        }).map(o -> Long.valueOf(o.toString().split(":")[0])).collect(Collectors.toList());

        // 根据id列表获取粉丝信息
        return collect.stream().map(followId -> {
                    GetUserVO getUserVO = userInfoService.getUserById(followId);
                    // 判断用户是否关注当前粉丝
                    Boolean follow = isFollow(userId, followId);
                    getUserVO.setFansFollow(follow);
                    return getUserVO;
                }).collect(Collectors.toList());
    }

}

